package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.utils.HtmlCleaner;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class _21CNNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		// http://finance.21cn.com/stock/headline/2012/11/22/13752775.shtml
		// http://auto.21cn.com/autonews/xinche/a/2012/1122/15/20031442.shtml
		if (url.indexOf("/tv/") != -1) {
			return false;
		}
		boolean bRet=false;
		String[] regex={
				"http://.*[.]21cn[.]com/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*[.]shtml",
				"http://.*[.]21cn[.]com/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*[.]shtml\\?fr=.*",
				"http://.*[.]21cn[.]com/.*/[0-9]{4}/[0-9]{4}/[0-9]{2}/[0-9]*[.]shtml",
				"http://.*[.]21cn[.]com/.*/[0-9]{4}/[0-9]{4}/[0-9]{2}/[0-9]*[.]shtml\\?fr=.*",
				"http://.*.21cn.com/.*/show/id/[0-9]*",
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		String html=urlMeta.getHtml();
		if (html == null) {
		}
		String title="";
		String content="";
		Long date=0l;
		int commentNum=0;
		int clickNum=0;
		NewsMeta meta=new NewsMeta();
		String url=urlMeta.getUrl();
		meta.setUrl(url);
		meta.setType(0);
		Document doc=Jsoup.parse(html);
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		title=doc.select("h1.title").text();
		if(StringUtils.isBlank(title)){
			title=doc.select(".show_post_dl strong").text();
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("h1").text();
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("h3").text();
		}
		if(StringUtils.isBlank(title)){
			title=doc.select(".link24p b").text();
		}

		/*
		 * 解析时间
		 */
		date=DateUtils.matchDate(doc.select("span.pubTime").text());
		if(date == null){
			date=DateUtils.matchDate(doc.select("div.fl,.link12Gray2").text());
		}
		if(date == null){
			date=JSoupUtils.matchDate(doc, "发表于");
		}
		if(date == null){
			date=DateUtils.matchDate(url);
		}
		String author=doc.select("div#article-cmt div.hd div.clearfix div.info a").text();
		if(author.matches("[0-9]+-.*-[0-9]+-[0-9]+")){
			author=StringUtils.substringBetween(author, "-", "-");
		}
		if(StringUtils.isBlank(author)){
			author = JSoupUtils.matchAuthor(doc, "投诉人:");
		}

		/*
		 * 解析内容
		 */

		doc.select(".page-box,a").remove();
		content=HtmlCleaner.getContentHtml(url,doc.select("div#article_text"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.gallery-descn"));
		}
		if(StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url, doc.select("#zoom"));
		}
		if(StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url, doc.select(".ts-content"));
		}
		if(StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url, doc.select(".summary"));
		}
		if(StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url, doc.select("dd:contains(投诉内容：) + dd"));
		}
		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);

		meta.setAuthor(author);
		return meta;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	public static void main(String[] args) {
		String url="http://news.21cn.com/social/shixiang/a/2015/1207/11/30349383.shtml";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		_21CNNewsAnalyse cnhubeiNewsAnalyse=new _21CNNewsAnalyse();
		System.out.println(cnhubeiNewsAnalyse.isDetailPage(url));
		boolean detailPage=cnhubeiNewsAnalyse.isDetailPage(url);
		if (detailPage) {
			NewsMeta parserHtml=cnhubeiNewsAnalyse.parserHtml(urlMeta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不符合正则");
		}
	}

	
	public boolean isNeedUpdate(){
		return true;
	}
}
